<?php

namespace app\admin\controller\free;

use app\common\controller\Backend;
use think\Cache;

/**
 * 免单活动配置
 *
 * @icon fa fa-circle-o
 */
class Config extends Backend
{

    /**
     * Config模型对象
     * @var \app\admin\model\free\Config
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\free\Config;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function shortlink()
    {
        // 获取表单参数
        $type = $this->request->param('type');


        $link = null;
        if ($type == 'seckill') {
            // 今天的日期
            $date = date('Y-m-d');
            $link = Cache::get('shortlink_seckill_' . $date);
            if (empty($link)) {
                $link = getMpShortLink("pages/home/index?type=seckill&date=" . $date, null, false);
                $expireTime = strtotime('23:59:59') - time();
                Cache::set('shortlink_seckill_' . $date, $link, $expireTime);
            }
        } elseif ($type == 'new_user') {
            $link = Cache::get('shortlink_new_user');
            if (empty($link)) {
                $link = getMpShortLink("pages/home/index", null, true);
                Cache::set('shortlink_new_user', $link, 0);
            }
        } elseif ($type == 'join_group') {
            $link = Cache::get('shortlink_join_group');
            if (empty($link)) {
                $link = getMpShortLink("pages/home/index?type=group", null, true);
                Cache::set('shortlink_join_group', $link, 0);
            }
        }else {
            $page_url = $this->request->param('page_url');
            $page_title = $this->request->param('page_title');
            $is_permanent = $this->request->param('is_permanent');
            $link = getMpShortLink( $page_url, $page_title, $is_permanent);
        }

        return [
            'link' => $link,
        ];

    }

    /**
     * 清楚短链缓存
     * @return void
     */
    public function cleanShortlinkCache()
    {
        $date = date('Y-m-d');
        Cache::rm('shortlink_new_user');
        Cache::rm('shortlink_join_group');
        Cache::rm('shortlink_seckill_' . $date);

        $this->success('清除成功');
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


}
